** Regressions
log using "$project_path/output/appendix_tables/Appendix_Table_D17.smcl", replace

use "$project_path/data/4_regdata/regdata", clear

local controls = ""
foreach v in growth_gdpc_penn inflation_imf unemployment_ilo trade_wb hdi democracy_index {
	gen mis_`v'_1lag = (`v'_1lag==.)
	replace `v'_1lag = 0 if mis_`v'_1lag==1
	local controls = "`controls' `v'_1lag mis_`v'_1lag"
}
order `controls', last

rdrobust diff_chix_econ runvar, all covs(`controls')
rdrobust std_diff_growth_gdpc_penn runvar, all covs(`controls')
rdrobust mstd_diff_inflation_imf runvar, all covs(`controls')
rdrobust mstd_diff_unemployment_ilo runvar, all covs(`controls')
rdrobust std_diff_trade_wb runvar, all covs(`controls')
rdrobust std_diff_hdi runvar, all covs(`controls')
rdrobust std_diff_democracy_index runvar, all covs(`controls')
rdrobust diff_general_chix runvar, all covs(`controls')

log close


** Building .tex table

#delimit ;
local outcomes  
		diff_chix_econ 
		std_diff_growth_gdpc_penn 
		mstd_diff_inflation_imf 
		mstd_diff_unemployment_ilo 
		std_diff_trade_wb 
		std_diff_hdi 
		std_diff_democracy_index 
		diff_general_chix
;
#delimit cr		

local model_count : word count `outcomes'

	// Initialize matrix of results and define value labels
	local ncols = `model_count'*2
	local nrows = 6
	//Prepare matrix of coefficients and other statistics
	mat Coefs = J(`nrows', `ncols', .)
		

//inizialize indices
local i = 0
local pval_col = `model_count'
foreach v in `outcomes'{
	local ++i 
	local ++pval_col
	rdrobust `v' runvar, all covs(`controls')
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	local N_`i': di %6.0f e(N)
	loca N_eff_`i': di %6.0f e(N_h_l) + e(N_h_r)
	local bw_`i': di %5.1f e(h_r)
	
	
	mat Coefs[1,`i'] = `b_`i''
	mat Coefs[2,`i'] = `se_`i''
	mat Coefs[3,`i'] = `p_`i''	
	mat Coefs[4,`i'] = `N_`i''
	mat Coefs[5,`i'] = `N_eff_`i''
	mat Coefs[6,`i'] = `bw_`i''
	
	mat Coefs[1,`pval_col'] = `p_`i''
	


}
mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
	// Clear the dataset, and use the matrix as dataset			

	clear
	svmat Coefs

	//	input variable names
	input str22 varname 
	"El. turn."
	"SE"
	"p-val"
	"N"
	"N eff."
	"Band."
	
	replace varname = "" if varname == "SE"
	order varname
	
	// Rename cols and labels
	forvalues n = 1/`model_count'{
		rename Coefs`n' Model`n'
	}
	
	//Cols where we store pvalues
	local n_min = `model_count' + 1
	local n_max = `model_count'*2
	local i = 1
	forvalues n = `n_min'/`n_max'{
		rename Coefs`n' P_Model`i'
		local ++i
	}

	label var varname ""
	label var Model1 "\textbf{Econ. perf.}"
	label var Model2 "GDP p.c. gr."
	label var Model3 "(Minus) Inflation"
	label var Model4 "(Minus) Unemp."
	label var Model5 "Trade"
	label var Model6 "\textbf{HDI}"
	label var Model7 "\textbf{Democ.}"
	label var Model8 "\textbf{General index}"
		
	// Convert to string and add options
	forvalues i = 1/`model_count'{
		tostring Model`i', replace force format(%04.3f)
		replace Model`i'= "" if Model`i'=="."
		//Add parenthesis to SE and P-val
		replace Model`i' = "(" + Model`i' + ")" if missing(varname) & Model`i' !=""	
		replace Model`i' = "[" + Model`i' + "]" if varname == "p-val" & Model`i' !=""	
		//Remove decimals to N obs
		replace Model`i' = substr(Model`i', 1, strpos(Model`i', ".")-1) if varname == "N"
		replace Model`i' = substr(Model`i', 1, strpos(Model`i', ".")-1) if varname == "N eff."
		
		replace Model`i' = substr(Model`i', 1, strpos(Model`i', ".")+1) if varname == "Band."

	}
	
	// Add stars
	local 3_star 0.01
	local 2_star 0.05
	local 1_star 0.10
	
	forvalues i = 1/8{
		
		replace Model`i' = Model`i' + "***" if P_Model`i' < `3_star'
		replace Model`i' = Model`i' + "**" if P_Model`i' < `2_star' & P_Model`i' > `3_star'
		replace Model`i' = Model`i' + "*" if P_Model`i' < `1_star' & P_Model`i' > `2_star'
		
		drop P_Model`i'
		
	}

	local title "Robustness to controlling for pre-election values of the outcomes"
	local label_key "tab:index_components_robustness_pre_controls"
	local output "$project_path/output/appendix_tables/Appendix_Table_D17.tex"
	local header "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8)"
	local footnote = "\emph{Notes}: This table reports results for the statistical procedure of Table \ref{tab:index_components_baseline}, adding controls for pre-election outcomes in the estimation. We use as controls the value of the main outcomes for the pre-election year. When one control is missing, we set it to zero, and we include also in the list of controls a set of dummies equal to 1 if a control is missing."

	texsave using "`output'", replace varlabels frag nofix hlines(3) location(h) ///
							  title("`title'")  ///
							  footnote("`footnote'") ///
							  marker("`label_key'") ///
							  headerlines("`header'")
			



